package com.dgut.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {

    private final RedisTemplate<String,String> redisTemplate;

    public RedisUtils(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    //读取缓存
    public String get(final String key){
        return redisTemplate.opsForValue().get(key);
    }

    //写入缓存
    public boolean set(final String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key,value);
            result =true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    //更新缓存
    public boolean getAndSet(final String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    //删除缓存
    public boolean delete(final String key){
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
